package com.vote.admin.job;

import cn.hutool.core.collection.CollectionUtil;
import com.vote.admin.util.OSSUtil;
import com.vote.common.entity.VoteOption;
import com.vote.common.entity.VoteProject;
import com.vote.common.service.VoteOptionService;
import com.vote.common.service.VoteProjectService;
import com.vote.common.utils.TimeUtilCustomize;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
@Slf4j
public class OSSCleanJob {

    @Autowired
    private VoteProjectService voteProjectService;

    @Autowired
    private VoteOptionService voteOptionService;

    @Autowired
    private OSSUtil ossUtil;

    /**
     * 每30分清理 阿里云OSS服务器 vote/project/文件夹下的垃圾图片
     */
    @Scheduled(cron = "0 */30 * * * ?")
    public void cloudHeadOSSCleanJob() {
        log.info("每天凌晨1点10分清理阿里云OSS服务器vote/project/文件夹下的垃圾图片任务开始...");
        List<VoteProject> voteProjectList = voteProjectService.list();

        if (CollectionUtil.isEmpty(voteProjectList)) {
            return;
        }

        //所有图片url集合
        List<String> urlList = voteProjectList.stream()
                .map(VoteProject::getProjectImg)
                .collect(Collectors.toList());


        List<String> stringList = ossUtil.list("vote/project");

        cleanOSS(urlList, stringList, "vote/project/");
        log.info("每30分清理阿里云OSS服务器vote/project/文件夹下的垃圾图片任务结束...");
    }

    /**
     * 每30分清理 阿里云OSS服务器 vote/option/文件夹下的垃圾图片
     */
    @Scheduled(cron = "0 */30 * * * ?")
    public void cloudMessageOSSCleanJob() {
        log.info("每30分清理阿里云OSS服务器vote/option/文件夹下的垃圾图片任务开始...");
        List<VoteOption> voteOptionList = voteOptionService.list();

        if (CollectionUtil.isEmpty(voteOptionList)) {
            return;
        }

        List<String> urlList = voteOptionList.stream()
                .map(VoteOption::getOptionImg)
                .collect(Collectors.toList());

        List<String> stringList = ossUtil.list("vote/option");

        cleanOSS(urlList, stringList, "vote/option/");
        log.info("每30分清理阿里云OSS服务器vote/option/文件夹下的垃圾图片任务结束...");
    }


    /**
     * 阿里云OSS文件清理 公用方法
     * @param urlList
     * @param stringList
     * @param objectName
     */
    private void cleanOSS(List<String> urlList, List<String> stringList, String objectName) {
        //时间
        String date = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_DAY_FORMAT, new Date());

        if (Objects.isNull(stringList) || stringList.isEmpty()) {
            log.info(date + objectName + "文件夹下暂无垃圾文件清理");
            return;
        }

        stringList = stringList.stream()
                .filter(v -> !StringUtils.equals(v, objectName))
                .map(v -> "http://gaoyiguan.cn/" + v)
                .collect(Collectors.toList());

        if (Objects.isNull(stringList) || stringList.isEmpty()) {
            log.info(date + objectName + "文件夹下暂无垃圾文件清理");
            return;
        }

        //两个集合取差集
        stringList.removeAll(urlList);

        if (Objects.isNull(stringList) || stringList.isEmpty()) {
            log.info(date + objectName + "文件夹下暂无垃圾文件清理");
            return;
        }

        stringList = stringList.stream()
                .map(v -> {
                    return v.split("http://gaoyiguan.cn/")[1];
                })
                .collect(Collectors.toList());

        ossUtil.deleteAll(stringList);
        log.info(date + objectName + "文件夹下垃圾文件清理完毕！");
    }

}
